
package com.hlkj.pay.controller.ad;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.ad.IAdPayOrderAppService;
import com.hlkj.pay.app.ad.IAdPayOrderCallService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.ad.AdPayOrderDto;
import com.hlkj.pay.dto.ad.query.AdPayOrderQueryDto;
import com.hlkj.pay.infrastructure.model.ad.AdInfoDO;
import com.hlkj.pay.infrastructure.model.ad.AdMediaDO;
import com.hlkj.pay.service.ad.IAdInfoService;
import com.hlkj.pay.service.ad.IAdMediaService;
import com.hlkj.pay.service.ad.IAdPayOrderService;
import com.hlkj.pay.vo.ad.req.AdOrderRefundApplyReq;
import com.hlkj.pay.vo.ad.req.AdPayOrderPageQueryReq;
import com.hlkj.pay.vo.ad.resp.AdPayOrderResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/10/23 11:36
 */
@Tag(name = "广告订单管理")
@RestController
@RequestMapping("/v1/ad/pay/order")
@RequiredArgsConstructor
@Slf4j
public class AdPayOrderController extends BaseController {

    private final IAdPayOrderService adPayOrderService;
    private final IAdPayOrderAppService adPayOrderAppService;

    private final IAdInfoService adService;

    private final IAdMediaService adMediaService;

    private final IAdPayOrderCallService payOrderCallService;

    @Operation(summary = "订单查询")
    @GetMapping("/page")
    CommonResult<PageResult<AdPayOrderResp>> queryPayOrderPage(AdPayOrderPageQueryReq adPayOrderPageQueryReq) {
        AdPayOrderQueryDto adInfoQueryDO = BeanUtil.copy(adPayOrderPageQueryReq, AdPayOrderQueryDto.class);
        processQueryDate(adPayOrderPageQueryReq, adInfoQueryDO);
        adInfoQueryDO.setMchOrderNo(null);
        adInfoQueryDO.setLikeOrderNo(adPayOrderPageQueryReq.getMchOrderNo());
        if (StringUtils.hasText(adPayOrderPageQueryReq.getName())) {
            adInfoQueryDO.setAdName(adPayOrderPageQueryReq.getName());
        }
        PageResult<AdPayOrderDto> page = adPayOrderService.queryPage(adInfoQueryDO);
        List<AdPayOrderDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, cn.hutool.core.bean.BeanUtil.copyToList(list, AdPayOrderResp.class));
    }

    @Operation(summary = "订单详情")
    @GetMapping("/detail")
    CommonResult<AdPayOrderResp> queryPayOrderDetail(@RequestParam("mchOrderNo") String mchOrderNo) {
        CommonResult<AdPayOrderResp> orderRespCommonResult = adPayOrderAppService.queryDetail(mchOrderNo);
        if (!orderRespCommonResult.isSuccess()) {
            return orderRespCommonResult;
        }
        AdPayOrderResp adPayOrderResp = orderRespCommonResult.getData();
        AdInfoDO adInfoDO = adService.queryById(adPayOrderResp.getAdId());
        AdMediaDO adMediaDO = adMediaService.queryById(adInfoDO.getMediaId());
        adPayOrderResp.setName(adInfoDO.getName());
        adPayOrderResp.setAdType(adInfoDO.getType());
        adPayOrderResp.setMediaName(adMediaDO.getName());
        return CommonResult.success(adPayOrderResp);
    }

    @Operation(summary = "手动回传")
    @PostMapping("/callBack")
    CommonResult<Void> callBackOrder(@RequestBody AdPayOrderPageQueryReq adPayOrderPageQueryReq) {
        return payOrderCallService.callBackOrder(adPayOrderPageQueryReq.getMchOrderNo());
    }

    @Operation(summary = "手动回传")
    @PostMapping("/callback/new")
    CommonResult<Void> callBackOrderNew(@RequestBody AdOrderRefundApplyReq adPayOrderPageQueryReq) {
        return payOrderCallService.callBackOrder(adPayOrderPageQueryReq.getMchOrderNo());
    }
}
